﻿using DLL.NET70.Infrastructure.Prism.MVVM;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Module.MS.Dialog.ViewModels
{
    public class PUC_SelectInjectorViewModel : CPDMVVMBase
    {
        public PUC_SelectInjectorViewModel()
        {
            _SeletedInjector = new ObservableCollection<bool>();
            for (int i = 0; i < 6; i++)
            {
                SeletedInjector.Add(true);
            }
        }

        #region property

        private ObservableCollection<bool> _SeletedInjector = new ObservableCollection<bool>();

        public ObservableCollection<bool> SeletedInjector
        {
            get { return _SeletedInjector; }
            set { SetProperty(ref _SeletedInjector, value); }
        }

        #endregion property

        #region IDialogAware 接口实现

        public override void CloseDialog()
        {
            DialogParameters keyValuePairs = new DialogParameters();
            string selected = BoolArrayToStr(SeletedInjector);
            keyValuePairs.Add("SelectedInjector", selected);
            DialogResult dialogResult = new DialogResult(ButtonResult.OK, keyValuePairs);
            RaiseRequestClose(dialogResult);
        }

        private string BoolArrayToStr(ObservableCollection<bool> array)
        {
            string str = "";
            for (int i = 0; i < array.Count(); i++)
            {
                int v = 0;
                if (array[i] == false)
                {
                    v = 0;
                }
                else
                {
                    v = 1;
                }
                if (i == 0)
                {
                    str = v.ToString();
                }
                else
                {
                    str = str + "-" + v.ToString();
                }
            }
            return str;
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            Message = parameters.GetValue<string>("SelectedInjector");
            string[] arr = Message.Split("-");
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == "0")
                {
                    SeletedInjector[i] = false;
                }
                else
                {
                    SeletedInjector[i] = true;
                }
            }
        }

        #endregion IDialogAware 接口实现

        #region 界面相应函数

        private bool _SelectedAllIsCheck = false;

        public bool SelectedAllIsCheck
        {
            get { return _SelectedAllIsCheck; }
            set { SetProperty(ref _SelectedAllIsCheck, value); }
        }

        private DelegateCommand _SelectedAllCommand;

        public DelegateCommand SelectedAllCommand =>
            _SelectedAllCommand ?? (_SelectedAllCommand = new DelegateCommand(SelectedAll));

        private void SelectedAll()
        {
            if (SelectedAllIsCheck == false)
            {
                for (int i = 0; i < 6; i++)
                {
                    SeletedInjector[i] = false;
                }
            }
            else
            {
                for (int i = 0; i < 6; i++)
                {
                    SeletedInjector[i] = true;
                }
            }
        }

        #endregion 界面相应函数
    }
}